package com.test.suanfa;

/**
 * 找出数组中乘积最大的3个数字
 * 考察点：线性扫描
 */
public class 乘积最大的三个数 {

    public static void main(String[] args) {
        int[] nums = {-4, 1, 2, 3, 4, 5, 6};
        System.out.println(test(nums));
        System.out.println(test1(nums));
    }

    public static int test1(int[] nums) {
        Integer min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        Integer max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MAX_VALUE;
        int max = 0;
        for (int i=0;i<nums.length;i++){
            int v = nums[i];
            if (v < min1){
                min2 = min1;
                min1 = v;
            }else if (v< min2){
                min2 = v;
            }

            if(v > max1){
                max3 = max2;
                max2 = max1;
                max1 = v;
            }else if (v > max2){
                max3 = max2;
                max2 = v;
            }else if(v > max3){
                max3 = v;
            }
        }
        max = Math.max((min2*min1*max1),(max1*max2*max3));
        return max;
    }


    public static int test(int[] nums) {
        int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;

        for (int x : nums) {
            if (x < min1) {
                // 如果比最小的还小
                min2 = min1;
                min1 = x;
            } else if (x < min2) {
                // 如果只比倒数第二个小
                min2 = x;
            }

            if (x > max1) {
                // 如果比最大的数字还大
                max3 = max2;
                max2 = max1;
                max1 = x;
            } else if (x > max2) {
                max3 = max2;
                max2 = x;
            } else if (x > max3) {
                max3 = x;
            }
        }
        System.out.println(min1 + "," + min2 + "," + max1 + "," + max2 + "," + max3);
        return 1;
    }
}
